package com.bing.jh.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.jh.entity.DzWzInstallMaintenanceStat;
import com.bing.jh.entity.dto.TZwArchivesDto;
import com.bing.jh.entity.vo.AreaVO;
import com.bing.jh.entity.vo.DzWzContrastVO;
import com.bing.jh.mapper.DzWzInstallMaintenanceStatMapper;
import com.bing.jh.service.DzWzInstallMaintenanceStatService;
import org.springframework.stereotype.Service;

import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangqilong
 * @description 针对表【dz_wz_install_maintenance_stat(装维成本统计)】的数据库操作Service实现
 * @createDate 2024-07-02 16:52:02
 */
@Service
public class DzWzInstallMaintenanceStatServiceImpl extends ServiceImpl<DzWzInstallMaintenanceStatMapper, DzWzInstallMaintenanceStat>
        implements DzWzInstallMaintenanceStatService {


    @Override
    public List<AreaVO> queryArea(TZwArchivesDto tZwArchives) {
        return baseMapper.queryArea(tZwArchives);
    }



    @Override
    public List<DzWzContrastVO> contrast(TZwArchivesDto tZwArchives) {
        tZwArchives.setMonth(tZwArchives.getMonth().replace("-", ""));
        Locale locale = Locale.CHINA;
        Collator collator = Collator.getInstance(locale);
        List<DzWzContrastVO> list = new ArrayList<>();
        if (CharSequenceUtil.isEmpty(tZwArchives.getCityCode())) {
            list = baseMapper.contrast(tZwArchives).stream()
                    .sorted(Comparator.comparing(o -> Optional.ofNullable(o.getAreaType()).orElse(""), collator))
                    .peek(o -> {
                        if (o.getAreaType() == null || "".equals(o.getAreaType())) {
                            o.setAreaType(null);
                        }
                    })
                    .collect(Collectors.toList());
        }
        if (CharSequenceUtil.isEmpty(tZwArchives.getCounty())) {
            List<DzWzContrastVO> countyStat = baseMapper.queryContrastCn(tZwArchives);
            List<DzWzContrastVO> finalList = list;
            countyStat.forEach(dzWzCostMatchStatVO1 -> {
                finalList.add(0, dzWzCostMatchStatVO1);
            });
        }
        return list;
    }

}




